package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.RedisConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.SetmealDao;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 体检套餐服务
 */
@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;
    @Autowired
    private JedisPool jedisPool;
/*新增套餐*/
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
      // 新增套餐信息,同时需要关联检查组
        setmealDao.add(setmeal);
           // 获取setmeal表的id
        Integer setmealId = setmeal.getId ();
          // 设置检查组与套餐表的关链表,调用setSetmealAndCheckgroup
        this.setSetmealAndCheckgroup(setmealId,checkgroupIds);
           // 将图片名称保存到Redis集合中
        String fileName = setmeal.getImg ();
        jedisPool.getResource().sadd( RedisConstant.SETMEAL_PIC_DB_RESOURCES,fileName);
    }
  // 设置套餐与检查组的多对多关系
    public void setSetmealAndCheckgroup(Integer setmealId, Integer[] checkgroupIds) {
            if (checkgroupIds !=null && checkgroupIds.length>0){
                for (Integer checkgroupId : checkgroupIds) {
                    Map<String, Integer> map = new HashMap <> ();
                    map.put ( "setmealId",setmealId );
                    map.put ( "checkgroupIds",checkgroupId );
                    setmealDao.setSetmealAndCheckGroup(map);
                }
            }
    }
   /*分页*/
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage ();
        Integer pageSize = queryPageBean.getPageSize ();
        String queryString = queryPageBean.getQueryString ();
        //调用分页插件
        PageHelper.startPage(currentPage,pageSize);
          /*分页查询条件方法*/
        Page<Setmeal> page = setmealDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());

    }
    // 根据id查询套餐
    public Setmeal findById(int id) {
      return setmealDao.findById(id);
    }
    //根据套餐ID查询当前套餐包含的检查组ID
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return setmealDao.findCheckItemIdsByCheckGroupId(id);
    }
    //编辑套餐
    @Override
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //修改套餐基本信息，操作检查组t_setmeal表
          setmealDao.edit(setmeal);
        //清理当前套餐关联的检查组，操作中间关系表t_setmeal_checkgroup表
        setmealDao.deleteAssocication(setmeal.getId ());
        //重新建立当前套餐和检查组的关联关系
        Integer setmealId = setmeal.getId ();
        // 设置检查组与套餐表的关链表,调用setSetmealAndCheckgroup
        this.setSetmealAndCheckgroup(setmealId,checkgroupIds);
    }
  /*删除*/
    @Override
    public void delete(Integer id) {
       setmealDao.delete(id);
    }

    @Override
    public List <Setmeal> findAll() {
        return setmealDao.findAll();
    }
 //根据套餐id查询套餐详情(套餐基本信息,套餐对应的检查组信息,检查组对应的检查项信息)
    @Override
    public Setmeal findByIds(int id) {
         return setmealDao.findByIds ( id );
    }
}
